package com.jdjw.modules.thirdpost.service;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jdjw.common.ResponseResult;
import com.jdjw.modules.busassetapply.entity.BusAssetApply;
import com.jdjw.modules.busassetapply.mapper.BusAssetApplyMapper;
import com.jdjw.modules.thirdpost.param.BusAssetApplyParam;
import com.jdjw.modules.thirdpost.param.ThirdPostInParm;
import com.jdjw.utils.CommonUtil;
import com.jdjw.utils.ConstantUtil;
import com.jdjw.utils.DateUtil;
import com.jdjw.utils.HttpRequestUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class BusAssetApplyParamServiceImpl extends ServiceImpl<BusAssetApplyMapper, BusAssetApply>
        implements IService<BusAssetApply> {

    @Autowired
    static BusAssetApplyMapper busAssetApplyMapper;

    @Autowired
    static ThirdPostExceptionServiceImpl thirdPostExceptionServiceImpl;

    @Resource
    public void setThirdPostExceptionServiceImpl(ThirdPostExceptionServiceImpl thirdPostExceptionServiceImpl) {
        BusAssetApplyParamServiceImpl.thirdPostExceptionServiceImpl = thirdPostExceptionServiceImpl;
    }

    @Resource
    public void setBusAssetApplyParamMapper(BusAssetApplyMapper busAssetApplyParamMapper) {
        busAssetApplyMapper = busAssetApplyParamMapper;
    }


    public static void doPostYs1(ThirdPostInParm thirdPostInParm) {
        String fangfaming = "doPostYs1";
        String shibai = "三资-发包合同应收款主表";
        String url = "";
        //全量
        if (ConstantUtil.ALL_TYPE.equals(thirdPostInParm.getMethodType())) {
            url = ConstantUtil.ALL_THIRD_POST + 30;
        } else if (ConstantUtil.ADD_TYPE.equals(thirdPostInParm.getMethodType())) {
            //增量必须有时间
            if (StringUtils.isEmpty(thirdPostInParm.getStartTime()) &&
                    StringUtils.isEmpty(thirdPostInParm.getEndTime())) {
                return;
            }
            url = ConstantUtil.ADD_THIRD_POST + 30;
        } else {
            //更新必须有时间
            if (StringUtils.isEmpty(thirdPostInParm.getStartTime()) &&
                    StringUtils.isEmpty(thirdPostInParm.getEndTime())) {
                return;
            }
            url = ConstantUtil.UPDATE_THIRD_POST + 30;
        }
        try {
            //查询
            List<BusAssetApplyParam> ys1 = busAssetApplyMapper.getYs1(thirdPostInParm);
            if (ys1 == null || ys1.size() < 1) {
                log.info("数据库查询数据为0条");
                return;
            }
            if (ys1.size() > 3000) {
                Collection<Collection<BusAssetApplyParam>> fgList = CommonUtil.subCollection(ys1, 2900);
                for (Collection<BusAssetApplyParam> obj : fgList) {
                    sendData((List<BusAssetApplyParam>) obj, url, fangfaming, shibai);
                }
            } else {
                sendData(ys1, url, fangfaming, shibai);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static void doPostYs2(ThirdPostInParm thirdPostInParm) {
        String fangfaming = "doPostYs2";
        String shibai = "三资-发包合同应收款表-子表";
        String url = "";
        //全量
        if (ConstantUtil.ALL_TYPE.equals(thirdPostInParm.getMethodType())) {
            url = ConstantUtil.ALL_THIRD_POST + 10;
        } else if (ConstantUtil.ADD_TYPE.equals(thirdPostInParm.getMethodType())) {
            //增量必须有时间
            if (StringUtils.isEmpty(thirdPostInParm.getStartTime()) &&
                    StringUtils.isEmpty(thirdPostInParm.getEndTime())) {
                return;
            }
            url = ConstantUtil.ADD_THIRD_POST + 10;
        } else {
            //更新必须有时间
            if (StringUtils.isEmpty(thirdPostInParm.getStartTime()) &&
                    StringUtils.isEmpty(thirdPostInParm.getEndTime())) {
                return;
            }
            url = ConstantUtil.UPDATE_THIRD_POST + 10;
        }
        try {
            //查询
            List<BusAssetApplyParam> ys2 = busAssetApplyMapper.getYs2(thirdPostInParm);
            if (ys2 == null || ys2.size() < 1) {
                log.info("数据库查询数据为0条");
                return;
            }
            if (ys2.size() > 3000) {
                Collection<Collection<BusAssetApplyParam>> fgList = CommonUtil.subCollection(ys2, 2900);
                for (Collection<BusAssetApplyParam> obj : fgList) {
                    sendData((List<BusAssetApplyParam>) obj, url, fangfaming, shibai);
                }
            } else {
                sendData(ys2, url, fangfaming, shibai);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void doPostSs1(ThirdPostInParm thirdPostInParm) {
        String fangfaming = "doPostSs1";
        String shibai = "三资-资产资源发包合同实收款明细表";
        String url = "";
        //全量
        if (ConstantUtil.ALL_TYPE.equals(thirdPostInParm.getMethodType())) {
            url = ConstantUtil.ALL_THIRD_POST + 11;
        } else if (ConstantUtil.ADD_TYPE.equals(thirdPostInParm.getMethodType())) {
            //增量必须有时间
            if (StringUtils.isEmpty(thirdPostInParm.getStartTime()) &&
                    StringUtils.isEmpty(thirdPostInParm.getEndTime())) {
                return;
            }
            url = ConstantUtil.ADD_THIRD_POST + 11;
        } else {
            //更新必须有时间
            if (StringUtils.isEmpty(thirdPostInParm.getStartTime()) &&
                    StringUtils.isEmpty(thirdPostInParm.getEndTime())) {
                return;
            }
            url = ConstantUtil.UPDATE_THIRD_POST + 11;
        }
        try {
            //查询
            List<BusAssetApplyParam> ss1 = busAssetApplyMapper.getSs1(thirdPostInParm);
            if (ss1 == null || ss1.size() < 1) {
                log.info("数据库查询数据为0条");
                return;
            }
            if (ss1.size() > 3000) {
                Collection<Collection<BusAssetApplyParam>> fgList = CommonUtil.subCollection(ss1, 2900);
                for (Collection<BusAssetApplyParam> obj : fgList) {
                    sendData((List<BusAssetApplyParam>) obj, url, fangfaming, shibai);
                }
            } else {
                sendData(ss1, url, fangfaming, shibai);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void doPostSs2(ThirdPostInParm thirdPostInParm) {
        String fangfaming = "doPostSs2";
        String shibai = "三资-资产资源发包合同实收款明细-实收子表";
        String url = "";
        //全量
        if (ConstantUtil.ALL_TYPE.equals(thirdPostInParm.getMethodType())) {
            url = ConstantUtil.ALL_THIRD_POST + 12;
        } else if (ConstantUtil.ADD_TYPE.equals(thirdPostInParm.getMethodType())) {
            //增量必须有时间
            if (StringUtils.isEmpty(thirdPostInParm.getStartTime()) &&
                    StringUtils.isEmpty(thirdPostInParm.getEndTime())) {
                return;
            }
            url = ConstantUtil.ADD_THIRD_POST + 12;
        } else {
            //更新必须有时间
            if (StringUtils.isEmpty(thirdPostInParm.getStartTime()) &&
                    StringUtils.isEmpty(thirdPostInParm.getEndTime())) {
                return;
            }
            url = ConstantUtil.UPDATE_THIRD_POST + 12;
        }
        try {
            //查询
            List<BusAssetApplyParam> ss2 = busAssetApplyMapper.getSs2(thirdPostInParm);
            if (ss2 == null || ss2.size() < 1) {
                log.info("数据库查询数据为0条");
                return;
            }
            if (ss2.size() > 3000) {
                Collection<Collection<BusAssetApplyParam>> fgList = CommonUtil.subCollection(ss2, 2900);
                for (Collection<BusAssetApplyParam> obj : fgList) {
                    sendData((List<BusAssetApplyParam>) obj, url, fangfaming, shibai);
                }
            } else {
                sendData(ss2, url, fangfaming, shibai);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static void doPostCzxx(ThirdPostInParm thirdPostInParm) {
        String fangfaming = "doPostCzxx";
        String shibai = "三资-资产资源处置信息登记表";
        String url = "";
        //全量
        if (ConstantUtil.ALL_TYPE.equals(thirdPostInParm.getMethodType())) {
            url = ConstantUtil.ALL_THIRD_POST + 9;
        } else if (ConstantUtil.ADD_TYPE.equals(thirdPostInParm.getMethodType())) {
            //增量必须有时间
            if (StringUtils.isEmpty(thirdPostInParm.getStartTime()) &&
                    StringUtils.isEmpty(thirdPostInParm.getEndTime())) {
                return;
            }
            url = ConstantUtil.ADD_THIRD_POST + 9;
        } else {
            //更新必须有时间
            if (StringUtils.isEmpty(thirdPostInParm.getStartTime()) &&
                    StringUtils.isEmpty(thirdPostInParm.getEndTime())) {
                return;
            }
            url = ConstantUtil.UPDATE_THIRD_POST + 9;
        }
        try {
            //查询
            List<BusAssetApplyParam> czxx = busAssetApplyMapper.getCzxx(thirdPostInParm);
            if (czxx == null || czxx.size() < 1) {
                log.info("数据库查询数据为0条");
                return;
            }
            if (czxx.size() > 3000) {
                Collection<Collection<BusAssetApplyParam>> fgList = CommonUtil.subCollection(czxx, 2900);
                for (Collection<BusAssetApplyParam> obj : fgList) {
                    sendData((List<BusAssetApplyParam>) obj, url, fangfaming, shibai);
                }
            } else {
                sendData(czxx, url, fangfaming, shibai);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    private static void sendData(List<BusAssetApplyParam> busAssetApplyParamList, String url, String fangfaming, String shibai) {
        ResponseResult result = new ResponseResult();
        ArrayList list = new ArrayList();
        for (BusAssetApplyParam busAssetApplyParam : busAssetApplyParamList
        ) {
            BusAssetApplyParam param = new BusAssetApplyParam();
            BeanUtils.copyProperties(busAssetApplyParam, param);
//            if (null == param.getSsze()) {
//                param.setSsze(0.00);
//            }
            param.setParent_id(param.getParent_id());
//            param.setYszqs(0);
            param.setDel_flag(busAssetApplyParam.getDel_flag());
            //状态先都塞值0，表示未删除
            param.setDel_flag(0);

            //转化为json字符串
            String jsonStr = JSONObject.toJSONString(param);
            //转化为json对象
            list.add(JSONObject.parseObject(jsonStr));
        }
        //发送(post)
        net.sf.json.JSONObject jsonObject = HttpRequestUtil.getHttpResponseJsonAllSSL(url, list);
        System.out.println("结果："+jsonObject);
        if (jsonObject != null) {
            if ("200".equals(jsonObject.get("code"))) {
                result.setCode(200);
                result.setMsg("SUCCESS");
                result.setData("推送完成");
                log.info(fangfaming + "成功"+list.size(), result);
            } else {
                //记录日志
                thirdPostExceptionServiceImpl.insertExceptionData(
                        fangfaming, shibai,
                        jsonObject.getString("message"),
                        DateUtil.formatDate(DateUtil.FORMAT_YMD, new Date().getTime()));
                result.setCode(500);
                result.setMsg("ERROR");
                result.setData(jsonObject.getString("message"));
                log.info(fangfaming + "推送失败", result);
            }
        } else {
            log.info(fangfaming + "推送失败", "第三方返回空");
        }

    }

}
